<?php

Request::getIncomeTime();

/**
 * 
 * Использовать php://input
 * 
 * @author philonov
 *
 */
class Request
{
	static function getParam()
	{
		if (isset($_GET[$name]))
		{
			return $_GET[$name];
		}
		elseif (isset($_POST[$name]))
		{
			return $_POST[$name];
		}
		elseif (isset($_COOCIES[$name]))
		{
			return $_COOCIES[$name];
		}
		elseif (isset($_SESSION[$name]))
		{
			return $_SESSION[$name];
		}
		
		return null;
	} 
	
	static function getIncomeTime()
    {
        static $result;
        if (is_null($result)) {
        	$result = microtime(true);
        }
        return $result;
    }

    static function getClientIP()
    {
        return $_SERVER['REMOTE_ADDR'];
    }

    static function getMethod()
    {
        return $_SERVER['REQUEST_METHOD'];
    }

    static function getThisHostName()
    {
        return $_SERVER['HTTP_HOST'];
    }

    static function getProtocol()
    {
        static $result = null;
        if (is_null($result)) {
            $result = isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on') ? 'https' : 'http';
        }
        return $result;
    }

    static function getClientEncodings()
    {
        static $result = null;
        if (is_null($result)) {
            $result = array();
            $encodings = isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? explode(',', $_SERVER['HTTP_ACCEPT_ENCODING']) : array();
            foreach ($encodings as $encoding) {
                $encoding = strtolower(trim($encoding));
                if (! empty($encoding)) {
                    $result[$encoding] = true;
                }
            }
            $result = array_keys($result);
        }
        return $result;
    }

    static function getAllowedCompressors()
    {
        return array_intersect(Application::outputEncodings(), self::getClientEncodings());
    }

    static function GetID()
    {
        static $id = null;
        if (is_null($id)) {
            $id = strval(round((microtime(true) * 1000))).'.'.rand(1111, 9999);
        }
        return $id;
    }

    public static function &o()
    {
        static $instance = null;
        if (empty($instance)) {
            $instance = new self();
        }
        return $instance;
    }

    private function __construct() {}

    public function __clone()
    {
        throw new ArchException('Clone singleton “'.__CLASS__.'” is not allowed');
    }
}

?>